import Vue from 'vue'
import VueRouter from "vue-router";
import Home from "../views/home/Home.vue";
import AuthRedirect from "../views/auth-redirect/auth-redirect";
import Article from "../views/article/Article.vue";
import Archive from "../views/archive/Archive.vue";
import Category from "../views/category/Category.vue";
import Link from "../views/link/Link.vue";
import About from "../views/about/About.vue";
import Message from "../views/message/Messsage.vue";
import ArticleList from "../views/article/ArticleList.vue";
import User from '../views/user/User'
import QQ from '../views/user/QQ'
import Tag from '../views/tag/Tag'
import Tags from '../views/tags/Tags'
import Edit from '../views/Edit'
import Update from '../views/update/Update'
import Album from '../views/album/Album'
import Phone from '../views/album/Photo'
import Map from '../views/map/Baidu'
import GaoDeMap from '../views/map/GaoDeMap'
import Manager from '../views/Manager'
import Video from '../views/video/Video'
import Download from '../views/download/Download'
import Chat from '../views/Chat'
import Test from '../views/test/test'
import VideoDetail from '../views/video/VideoDetail'
Vue.use(VueRouter)

// 解决报错
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push (location, onResolve, onReject) {
    if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
    return originalReplace.call(this, location).catch(err => err)
}

Vue.use(VueRouter);

const routes = [

  /*  {
        path: '/manager',
        name: 'Manager',
        redirect: "/echarts",
        component:Manager,

        children: [
            {path: '/echarts', name: '大数据面板', component: () => import("../views/Echarts"),},
            {path: '/person', name: '首页', component: () => import("../views/Person"),},
            {path: '/file', name: '文件管理', component: () => import("../views/File"),},
            {path: '/articless', name: '文章管理', component: () => import("../views/Articles"),},
            {path: '/info', name: '登录信息', component: () => import("../views/Infos"),},
            {path: '/sign', name: '签到记录', component: () => import("../views/Sign"),},
            {path: '/report', name: '举报记录', component: () => import("../views/Report"),},
            {path: '/role', name: '角色管理', component: () => import("../views/Role"),},
            {path: '/userRole', name: '用户角色', component: () => import("../views/UserRole"),},
            {path: '/permission', name: '权限菜单', component: () => import("../views/Permission"),},

        ]
    },*/

  {
    path: "/",
    component: Home,
    meta: {
      title: " 茶杯头 博客"
    }
  },
    {
        path: "/authRedirect",
        name: 'auth-redirect',
        component: AuthRedirect,
        meta: {
            title: " 茶杯头 博客"
        }
    },
  {
    path: "/articles",
    component: Article
  },
    {
        path: "/tags",
        component: Tag,
        meta: {
            title: "标签"
        }
    },

    {
        path: "/test",
        component: Test,
        meta: {
            title: "测试"
        }
    },

    {
        path: "/baiduMap",
        component: Map,
        meta: {
            title: "百度地图"
        }
    },
    {
        path: "/gaoDeMap",
        component: GaoDeMap,
        meta: {
            title: "高德地图"
        }
    },
    {
        path: "/album",
        component: Album,
        meta: {
            title: "相册"
        }
    },

    {
        path: "/download",
        component: Download,
        meta: {
            title: "下载"
        }
    },
    {
        path: "/phone",
        component: Phone,
        meta: {
            title: "图片"
        }
    },
    {
        path: "/articleList",
        component: ArticleList,
        meta: {
            title: "文章列表"
        }
    },
    {
        path: "/video",
        component: Video,
        meta: {
            title: "追剧达人"
        }
    },
    {
        path: "/videoDetail",
        component: VideoDetail,
        meta: {
            title: "视频"
        }
    },

    {
        path: "/tag",
        component: Tags,
        meta: {
            title: "标签"
        }
    },
    {
        path: "/user",
        component: User,
        meta: {
            title: "个人中心"
        }
    },
    {
        path: "/qq/user",
        component: QQ,
        meta: {
            title: "个人中心"
        }
    },
    {
        path: "/edit",
        component: Edit,
        meta: {
            title: "撰写博客"
        }
    },
    {
        path: "/update",
        component: Update,
        meta: {
            title: "更新博客"
        }
    },


    {
    path: "/archives",
    component: Archive,
    meta: {
      title: "时间链"
    }
  },
  {
    path: "/categories",
    component: Category,
    meta: {
      title: "分类"
    }
  },
  {
    path: "/links",
    component: Link,
    meta: {
      title: "友链列表"
    }
  },
    {
        path: "/chat",
        component: Chat,
        meta: {
            title: "聊天室"
        }
    },
  {
    path: "/about",
    component: About,
    meta: {
      title: "关于我"
    }
  },
  {
        path: "/message",
        component: Message,
        meta: {
            title: "留言板"
        },

  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,

  routes
});


//动态路由，防止刷新产生空白页面
activeRouter()

function activeRouter() {
    const userStr = sessionStorage.getItem("user")
    if (userStr) {
        const user = JSON.parse(userStr)
        let root = {
            path: '/manager',
            name: 'Manager',
            redirect: "/detail",
            component:Manager,
            children: []
        }
        user.permissions.forEach(p => {
            let obj = {
                path: p.path,
                name: p.name,
                component: () => import("@/views/" + p.name)
            };
            root.children.push(obj)
        })
        if (router) {
            router.addRoute(root)
        }
    }
}



export default router;
